<?php
namespace app\zhihui\controller;

use app\common\model\ZhNews as ZhNewsModel;
use app\home\controller\Base;
use think\Db;
/**
 * 高僧大德控制器
 * Class Zhnews
 * @package app\index\controller
 */
class Zhnews extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhNewsModel;
        $this->getPageAndSize($param);
        $map = [];
        try {
            $map['states'] = ['neq', 3];
            $map['comid'] = $param['comid'];
            $map['cid'] = $param['cate'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size, 'id,title,img,create_time,read_num,jread_num');
            foreach($lists as &$v){
                $v['addtime'] = date('Y-m-d',strtotime($v['create_time']));
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [get  获取一条]
     * @return json
     * @author [C。]
     */
    public function get()
    {
        $param = input('param.');
        $model = new ZhNewsModel;
        try{
            $data = $model->findData($param['id'],'id','title,create_time,read_num,jread_num,content');
            $data['addtime'] = date('Y-m-d',strtotime($data['create_time']));
            Db::name('zh_news')->where('id',$param['id'])->setInc('read_num');
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
}